<?php
//自动生成,请根据需要修改
namespace app\topic\controller;

use app\common\controller\Base;
use think\Db;
use think\db\Where;
use think\Validate;

/**
 * @title 帖子页面（模块06-01）
 * @description 帖子说明
 * @group 帖子（06）
 * @header name:model_id require:0 default:06-01 desc:模块
 * @groupremark 已使用错误码：1-06-01-0001 校验失败 1-06-01-0002 添加失败 1-06-01-0003 编辑校验失败 1-06-01-0004 编辑失败 1-06-01-0005 获取详情失败  1-06-01-0006 获取列表失败 1-06-01-0007获取单个详情失败 1-06-01-0008 删除失败
 */
class Topic extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加帖子
     * @description 添加帖子
     * @author 李锐志
     * @url /topic/topic/add
     * @method *
     * @header name:token require:1 default: desc:token
     * @param name:title type:varchar require:0 other: desc:标题
     * @param name:is_new type:tinyint require:0 other: desc:是否新帖
     * @param name:view_num type:int require:0 other: desc:浏览量
     * @param name:discuss_num type:int require:0 other: desc:评论量
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:update_time type:int require:0 other: desc:更新时间
     * @param name:img_url type:text require:0 other: desc:图片路径
     * @param name:content type:text require:0 other: desc:内容
     * @param name:category_id type:int require:0 other: desc:分类id
     * @param name:topic_type type:enum require:0 other: desc:帖子类型.special_topic:特殊帖子;common_topic:普通帖子
     * @param name:is_mentality type:enum require:0 other: desc:是否是心理贴
     * @param name:main_img_url type:varchar require:0 other: desc:封面图
     * @param name:annotation type:varchar require:0 other: desc:注释
     * @return data:帖子@!
     * @data    id:id    title:标题    is_new:是否新帖    view_num:浏览量    discuss_num:评论量    user_id:用户id    create_time:创建时间    update_time:更新时间    img_url:图片路径    content:内容    category_id:分类id    topic_type:帖子类型.special_topic:特殊帖子;common_topic:普通帖子    main_img_url:封面图    annotation:注释
     * @return error_id:1-06-01-0001 校验失败  1-06-01-0002 添加失败
     */
    public function add()
    {
        $data = input();
        $data['create_time'] = time();
        $topic_img_data = array();

        if (!empty($data['topic_img_url'])){
            $topic_img_data['url'] = $data['topic_img_url'];
            $topic_img_data['desc'] = $data['topic_img_desc'];
            unset($data['topic_img_url']);
            unset($data['topic_img_desc']);
        }

        $model = new \app\topic\model\Topic();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);

            if ($msg['data']['topic_type'] === 'special_topic' && !empty($topic_img_data['url'])){
                $img_data = array();
                $img_data['topic_id'] = $msg['data']['id'];
                foreach ($topic_img_data['url'] as $k => $v){
                    $img_data['img_url'] = $v;
                    $img_data['img_notation'] = $topic_img_data['desc'][$k];
                    $res = Db::name('comment_img')->insert($img_data);
                }
            }

            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-06-01-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑帖子
     * @description 编辑帖子
     * @author 李锐志
     * @url /topic/topic/edit
     * @method *
     * @header name:token require:1 default: desc:token
     * @param name:id type:int require:0 other: desc:id
     * @param name:title type:varchar require:0 other: desc:标题
     * @param name:is_new type:tinyint require:0 other: desc:是否新帖
     * @param name:view_num type:int require:0 other: desc:浏览量
     * @param name:discuss_num type:int require:0 other: desc:评论量
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:update_time type:int require:0 other: desc:更新时间
     * @param name:img_url type:text require:0 other: desc:图片路径
     * @param name:content type:text require:0 other: desc:内容
     * @param name:category_id type:int require:0 other: desc:分类id
     * @param name:topic_type type:enum require:0 other: desc:帖子类型.special_topic:特殊帖子;common_topic:普通帖子
     * @param name:is_mentality type:enum require:0 other: desc:是否是心理贴
     * @param name:main_img_url type:varchar require:0 other: desc:封面图
     * @param name:annotation type:varchar require:0 other: desc:注释
     * @return data:帖子@!
     * @data    id:id    title:标题    is_new:是否新帖    view_num:浏览量    discuss_num:评论量    user_id:用户id    create_time:创建时间    update_time:更新时间    img_url:图片路径    content:内容    category_id:分类id    topic_type:帖子类型.special_topic:特殊帖子;common_topic:普通帖子    main_img_url:封面图    annotation:注释
     * @return error_id:1-06-01-0003 校验失败 1-06-01-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $data['update_time'] = time();

        $topic_type = $data['topic_type'];
        if ($topic_type==="special_topic"){
            $topic_img_data = array();
            $del_img_id = array();
            if (!empty($data['topic_img_url'])){
                $topic_img_data['url'] = $data['topic_img_url'];
                $topic_img_data['desc'] = $data['topic_img_desc'];
                $topic_img_data['id'] = $data['topic_img_id'];
                $del_img_id = $data['del_img_id'];
            }
        }
        unset($data['topic_img_url']);
        unset($data['topic_img_desc']);
        unset($data['topic_img_id']);
        unset($data['del_img_id']);
        unset($data['topic_type']);

        $validate = new \app\topic\validate\Topic();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-06-01-0003';
            return $this->errorJson($msg);
        }
        $model = new \app\topic\model\Topic();
        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            if ($topic_type === 'special_topic'){
                Db::name('comment_img')->delete($del_img_id);
                foreach ($topic_img_data['url'] as $k => $v){
                    if ($topic_img_data['id'][$k] != ''){
                        Db::name('comment_img')->update([
                            'id'  => $topic_img_data['id'][$k],
                            'img_notation' => $topic_img_data['desc'][$k],
                            'id' => $topic_img_data['id'][$k]
                        ]);
                    }else{
                        Db::name('comment_img')->insert([
                            'topic_id' => $data['id'],
                            'img_url' => $topic_img_data['url'][$k],
                            'img_notation' => $topic_img_data['desc'][$k]
                        ]);
                    }
                }
            }
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-06-01-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除帖子
     * @description 删除帖子
     * @author 李锐志
     * @url /topic/topic/delete
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-06-01-0008 删除失败
     */
    public function delete()
    {
        $id = input('id');
        $model = new \app\topic\model\Topic();
        $flag = $model->destroy($id);
        if ($flag) {
            Db::name('comment')->where('topic_id', '=', $id)->delete();
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-06-01-0008';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 单个查询帖子
     * @description 单个查询帖子
     * @author 李锐志
     * @url /topic/topic/info
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:id type:int require:0 other: desc:id
     * @param name:title type:varchar require:0 other: desc:标题
     * @param name:is_new type:tinyint require:0 other: desc:是否新帖
     * @param name:view_num type:int require:0 other: desc:浏览量
     * @param name:discuss_num type:int require:0 other: desc:评论量
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:update_time type:int require:0 other: desc:更新时间
     * @param name:img_url type:text require:0 other: desc:图片路径
     * @param name:content type:text require:0 other: desc:内容
     * @param name:category_id type:int require:0 other: desc:分类id
     * @param name:topic_type type:enum require:0 other: desc:帖子类型.special_topic:特殊帖子;common_topic:普通帖子
     * @param name:is_mentality type:enum require:0 other: desc:是否是心理贴
     * @param name:main_img_url type:varchar require:0 other: desc:封面图
     * @param name:annotation type:varchar require:0 other: desc:注释
     * @return data:帖子@!
     * @data    id:id    title:标题    is_new:是否新帖    view_num:浏览量    discuss_num:评论量    user_id:用户id    create_time:创建时间    update_time:更新时间    img_url:图片路径    content:内容    category_id:分类id    topic_type:帖子类型.special_topic:特殊帖子;common_topic:普通帖子    main_img_url:封面图    annotation:注释
     * @return error_id:1-06-01-0005 校验失败 1-06-01-0007 获取失败
     */
    public function info()
    {
        $model = new \app\topic\model\Topic();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\topic\validate\Topic();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-06-01-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\topic\model\Topic();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-06-01-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表帖子
     * @description 查询列表帖子
     * @author 宋晓文
     * @url /topic/topic/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:帖子@
     * @list    id:id    title:标题    is_new:是否新帖    view_num:浏览量    discuss_num:评论量    user_id:用户id    create_time:创建时间    update_time:更新时间    img_url:图片路径    content:内容    category_id:分类id    category_name:分类名称    topic_type:帖子类型.special_topic:特殊帖子;common_topic:普通帖子    main_img_url:封面图    annotation:注释
     */
    public function get_list()
    {
        $model = new \app\topic\model\Topic();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);//前端传过来的条件
        $where = array();
        foreach ($data as $k => $v) {
            if ($v === 0) {
                continue;
            }
            if ($v == null) {
                unset($data[$k]);
            }
        }
        foreach ($data as $k => $v) {
            if ($k === 'create_time' || $k === 'update_time') {
                array_push($where, [$k, '>=', ceil($data[$k][0] / 1000)]);
                array_push($where, [$k, '<=', ceil($data[$k][1] / 1000)]);
                continue;
            }
            if ($k === 'category_name' || $k === 'id' || $k === 'is_new' || $k === 'view_num' || $k === 'category_id' || $k === 'topic_type' || $k === 'is_mentality') {
                array_push($where, [$k, '=', $data[$k]]);
                continue;
            }
            if (isset($data[$k]) && $data[$k] && !empty($data[$k]) && $k != 'user_id') {
                array_push($where, [$k, 'like', "%{$data[$k]}%"]);
            }
        }

        //根据分类名查询分类ID
        if (isset($data['category_name']) && $data['category_name'] && !empty($data['category_name'])) {
            $category_map['name'] = array('like', $data['category_name']);
            $category_id = Db::name('topic_category')->where(new Where($category_map))->column('id');
            array_push($where, ['category_id', '=', $category_id]);
        }
        if(isset($data['user_id']) && $data['user_id'] && !empty($data['user_id'])) {
            $user_map['nickname'] = array('like', "%{$data['user_id']}%");//用户名
            $user_ids = Db::table('zb_user.tp_user u')->where(new Where($user_map))->column('id');
            array_push($where, ['user_id', 'in', $user_ids]);
            $msg['ss'] = $where;
        }

        //获取列表数据
        $list = $model->where($where)->order('id', 'desc')->paginate();
        $cats = Db::name('topic_category')->select();
        $uname = Db::table('zb_user.tp_user')->select();
        foreach ($list as $k => $v) {
            foreach ($cats as $key => $value) {
                if ($v['category_id'] === $value['id']) {
                    $list[$k]['category_name'] = $value['name'];
                    break;
                }}
                foreach ($uname as $key => $value) {
                if ($v['user_id'] === $value['id']) {
                    $list[$k]['user_name'] = $value['nickname'];
                    break;
                }
            }
            $list[$k]['topic_img']=Db::name('comment_img')->where('topic_id',$list[$k]['id'])->select();
        }

        $msg['list'] = $list;

        return $this->successJson($msg);
    }

    /**
     * @title 前台通过id获取用户帖子
     * @description 前台通过id获取用户帖子
     * @author 李锐志
     * @url /topic/topic/get_user_topic
     * @method *
     * @param name:id require:1 default: desc:id
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     */
    public function get_user_topic()
    {
        $data = input();
        $result = Db::name('topic')
            ->where('user_id', $data['id'])
            ->order('id', 'desc')
            ->paginate();

        $msg['msg'] = '个人帖子获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台个人中心帖子
     * @description 前台个人中心帖子
     * @author 李锐志
     * @url /topic/topic/personal_center_topic
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     */
    public function personal_center_topic()
    {
        $result = Db::name('topic')
            ->where('user_id', USERID)
            ->order('id', 'desc')
            ->paginate();

        $msg['msg'] = '个人中心帖子获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台搜索页面帖子分类
     * @description 前台搜索页面帖子分类
     * @author 李锐志
     * @url /topic/topic/search_topic_category
     * @method *
     */
    public function search_topic_category()
    {
        $result = Db::name('topic_category')
            ->where('parent_id', 0)
            ->where('status', 1)
            ->field('id,name')
            ->select();

        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台搜索页面快捷搜索
     * @description 前台搜索页面快捷搜索
     * @author 李锐志
     * @url /topic/topic/shortcut_search
     * @param name:id require:1 default: desc:分类id
     * @method *
     */
    public function shortcut_search()
    {
        $id = input('id');

        $category = Db::name('topic_category')
            ->where('status', 1)
            ->where('id', $id)
            ->find();
        if (!$category) {
            $msg['msg'] = '该分类不存在或已被禁用!';
            return $this->errorJson($msg);
        }

        $son_category = Db::name('topic_category')
            ->where('parent_id', $id)
            ->where('status', 1)
            ->select();

        foreach ($son_category as $k => $v) {
            $son_category[$k]['topic'] = Db::name('topic')
                ->where('category_id', $v['id'])
                ->order('update_time', 'desc')
                ->limit(3)
                ->select();

            foreach ($son_category[$k]['topic'] as $key => $value) {
                $son_category[$k]['topic'][$key]['user'] = get_curl('user', 'user/get_user_information', ['id' => $value['user_id']])['data'];
            }
        }

        $category['son_category'] = $son_category;
        $msg['msg'] = '获取成功';
        $msg['data'] = $category;
        return $this->successJson($msg);
    }

    /**
     * @title 前台搜索页面详细搜索
     * @description 前台搜索页面详细搜索
     * @author 李锐志
     * @url /topic/topic/detail_search
     * @param name:keyword require:1 default: desc:关键字
     * @method *
     */
    public function detail_search()
    {
        $data = input();
        $category = Db::name('topic_category')
            ->where('parent_id', 0)
            ->where('status', 1)
            ->field('id,name')
            ->select();

        foreach ($category as $k => $v) {
            $son_category = Db::name('topic_category')
                ->where('parent_id', $v['id'])
                ->where('status', 1)
                ->field('id,name')
                ->select();
            if (!empty($son_category)) {
                foreach ($son_category as $key => $value) {
                    $son_category[$key]['topic'] = Db::name('topic')
                        ->where('category_id', $value['id'])
                        ->where('title', 'like', "%{$data['keyword']}%")
                        ->order('update_time', 'desc')
                        ->field('is_new,img_url,content', true)
                        ->limit(3)
                        ->select();
                    foreach ($son_category[$key]['topic'] as $key2 => $value2) {
                        $son_category[$key]['topic'][$key2]['user'] = get_curl('user', 'user/get_user_information', ['id' => $value2['user_id']])['data'];
                    }
                }
            } else {
                $son_category['topic'] = Db::name('topic')
                    ->where('category_id', $v['id'])
                    ->where('title', 'like', "%{$data['keyword']}%")
                    ->order('update_time', 'desc')
                    ->field('is_new,img_url,content', true)
                    ->limit(3)
                    ->select();
                foreach ($son_category['topic'] as $key => $value) {
                    $son_category['topic'][$key]['user'] = get_curl('user', 'user/get_user_information', ['id' => $value['user_id']])['data'];
                }
            }
            $category[$k]['son_category'] = $son_category;
        }

        $msg['msg'] = '获取成功';
        $msg['data'] = $category;
        return $this->successJson($msg);
    }

    /**
     * @title 前台搜索页面详细搜索更多
     * @description 前台搜索页面详细搜索更多
     * @author 李锐志
     * @url /topic/topic/detail_search_more
     * @param name:id require:1 default: desc:分类id
     * @param name:keyword require:1 default: desc:关键字
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @method *
     */
    public function detail_search_more()
    {
        $data = input();
        $id = input('id');
        $result = Db::name('topic')
            ->where('category_id', $id)
            ->where('title', 'like', "%{$data['keyword']}%")
            ->order('update_time', 'desc')
            ->paginate();
        $result = $result->toArray();

        foreach ($result['data'] as $k => $v) {
            $result['data'][$k]['user'] = get_curl('user', 'user/get_user_information', ['id' => $v['user_id']])['data'];
        }

        $msg['msg'] = '获取成功';
        $msg['list'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台心理页面
     * @description 前台心理页面
     * @author 李锐志
     * @url /topic/topic/get_mentality
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @method *
     */
    public function get_mentality()
    {
        $result = Db::name('topic')
            ->where('is_mentality', 1)
            ->order('update_time', 'desc')
            ->paginate();

        $result = $result->toArray();

        foreach ($result['data'] as $k => $v) {
            $result['data'][$k]['user'] = get_curl('user', 'user/get_user_information', ['id' => $v['user_id']])['data'];
        }

        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台新帖页面
     * @description 前台新帖页面
     * @author 李锐志
     * @url /topic/topic/new_topic
     */
    public function new_topic()
    {
        $category = Db::name('topic_category')
            ->where('parent_id', 0)
            ->where('status', 1)
            ->select();

        /**
         * 数组格式
         * $array = [
         *    '别墅' => [1],
         *    '自建' => [2],
         *    '平层' => [3,4]
         * ];
         */

        $array = [];
        foreach ($category as $k => $v) {
            $son_category = Db::name('topic_category')
                ->where('parent_id', $v['id'])
                ->where('status', 1)
                ->select();
            foreach ($son_category as $key => $value) {
                $array[$value['name']]['category_name'] = $value['name'];
                //使用分类名来分组存放分类id
                $array[$value['name']]['category_id'][] = $value['id'];
            }
        }
        //将关联数组转为索引数组
        $array = array_values($array);

        foreach ($array as $k => $v) {
 if($v['category_name'] === '家与情感')
                continue;
            $result[$k]['category_name'] = $v['category_name'];
            $result[$k]['category_id'] = json_encode($v['category_id']);
            $result[$k]['topic'] = Db::name('topic')
                ->where('category_id', 'in', $v['category_id'])
                ->where('is_new', 1)
                ->order('id', 'desc')
                ->limit(7)
                ->select();
        }
        $topic = Db::name('topic')
            ->where('is_mentality', 1)
            ->where('is_new', 1)
            ->order('id', 'desc')
            ->limit(7)
            ->select();
        foreach ($topic as $k => $v) {
            $topic[$k]['user'] = get_curl('user', '/user/get_user_information', ['id' => $v['user_id']])['data'];
        }
        $result[] = [
            'category_name' => '情感',
            'topic' => $topic
        ];

        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台新帖更多接口
     * @description 前台新帖更多接口
     * @author 李锐志
     * @url /topic/topic/new_topic_more
     * @param name:category_id_array require:1 default: desc:分类id集合(例如:[1,2,3])
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     */
    public function new_topic_more()
    {
        $data = input();
        $category_id_array = json_decode($data['category_id_array'], true);

        $result = Db::name('topic')
            ->where('category_id', 'in', $category_id_array)
            ->where('is_new', 1)
            ->order('id', 'desc')
            ->paginate();
        $result = $result->toArray();

        foreach ($result['data'] as $k => $v) {
            $result['data'][$k]['user'] = get_curl('user', '/user/get_user_information', ['id' => $v['user_id']])['data'];
        }

        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台论坛页面
     * @description 前台论坛页面
     * @author 李锐志
     * @url /topic/topic/forum_topic
     */
    public function forum_topic()
    {
        $category = Db::name('topic_category')
            ->where('parent_id', 0)
            ->where('status', 1)
            ->select();

        /**
         * 数组格式
         * $array = [
         *    '别墅' => [1],
         *    '自建' => [2],
         *    '平层' => [3,4]
         * ];
         */

        $array = [];
        foreach ($category as $k => $v) {
            $son_category = Db::name('topic_category')
                ->where('parent_id', $v['id'])
                ->where('status', 1)
                ->select();
            foreach ($son_category as $key => $value) {
                $array[$value['name']]['category_name'] = $value['name'];
                //使用分类名来分组存放分类id
                $array[$value['name']]['category_id'][] = $value['id'];
            }
        }
        //将关联数组转为索引数组
        $array = array_values($array);

        foreach ($array as $k => $v) {
            $result[$k]['category_name'] = $v['category_name'];
            $result[$k]['category_id'] = json_encode($v['category_id']);
            $result[$k]['topic'] = Db::name('topic')
                ->where('category_id', 'in', $v['category_id'])
                ->order('update_time', 'desc')
                ->limit(7)
                ->select();
        }

        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台论坛更多接口
     * @description 前台论坛更多接口
     * @author 李锐志
     * @url /topic/topic/forum_topic_more
     * @param name:category_id_array require:1 default: desc:分类id集合(例如:[1,2,3])
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     */
    public function forum_topic_more()
    {
        $data = input();
        $category_id_array = json_decode($data['category_id_array'], true);

        $result = Db::name('topic')
            ->where('category_id', 'in', $category_id_array)
            ->order('update_time', 'desc')
            ->paginate();
        $result = $result->toArray();

        foreach ($result['data'] as $k => $v) {
            $result['data'][$k]['user'] = get_curl('user', '/user/get_user_information', ['id' => $v['user_id']])['data'];
        }

        $msg['msg'] = '获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台获取普通帖子分类
     * @description 前台获取普通帖子分类
     * @author 李锐志
     * @url /topic/topic/common_topic_category
     */
    public function common_topic_category()
    {
        $result = Db::name('topic_category')
            ->where('parent_id', 0)
            ->where('status', 1)
            ->select();

        foreach ($result as $k => $v) {
            if ($v['name'] === '设计博弈') {
                unset($result[$k]);
                continue;
            }
            if ($v['name'] === '家与情感') {
                continue;
            }
            $result[$k]['son_category'] = Db::name('topic_category')
                ->where('parent_id', $v['id'])
                ->where('status', 1)
                ->select();
        }
        $result = array_values($result);


        $msg['msg'] = '普通帖子分类获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台普通帖子发帖
     * @description 前台普通帖子发帖
     * @author 李锐志
     * @url /topic/topic/common_topic_publish
     * @param name:token require:1 default: desc:token
     * @param name:title require:1 default: desc:标题
     * @param name:category_id require:1 default: desc:分类id
     * @param name:content require:1 default: desc:内容
     * @param name:main_img_url require:1 default: desc:封面
     */
    public function common_topic_publish()
    {
        $data = input();
        $title_len = mb_strlen($data['title']);
        if ($title_len < 1 || $title_len > 60) {
            $msg['msg'] = '发帖失败,标题长度:1~60';
            return $this->errorJson($msg);
        }
        $validate = new Validate([
            'token' => 'require',
            'title' => 'require',
            'category_id' => 'require',
            'content' => 'require',
            'main_img_url' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->successJson($msg);
        }
        if ($data['category_id'] === '16') {
            $is_mentality = 1;
        } else {
            $is_mentality = 0;
        }

        $insert = [
            'title' => $data['title'],
            'user_id' => USERID,
            'content' => $data['content'],
            'category_id' => $data['category_id'],
            'topic_type' => 'common_topic',
            'main_img_url' => $data['main_img_url'],
            'is_mentality' => $is_mentality,
            'create_time' => time(),
            'update_time' => time()
        ];

        Db::name('topic')
            ->insert($insert);

        //发帖添加积分
        get_curl('user', '/user/topic_add_integral', ['token' => $data['token']]);

        $msg['msg'] = '普通帖子发帖成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台普通帖子详情
     * @description 前台普通帖子详情
     * @author 李锐志
     * @url /topic/topic/common_topic_details
     * @param name:user_id require:0 default: desc:用户id
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页个数
     */
    public function common_topic_details()
    {
        $data = input();

        //防止恶意刷浏览量，使用session来判断当前用户是否第一次浏览
        if(!is_dir('./tmp/'))mkdir ('./tmp/', 0700);
        session_save_path('./tmp/');
        session_start();
        if (empty($_SESSION['noRepeatId']) || $_SESSION['noRepeatId'] !== $_SERVER['REMOTE_ADDR'] . $data['topic_id']) {
            Db::name('topic')
                ->where('id', $data['topic_id'])
                ->setInc('view_num', 1);
            $_SESSION['noRepeatId'] = $_SERVER['REMOTE_ADDR'] . $data['topic_id'];
        }

        $topic_id = $data['topic_id'];

        $result = Db::name('topic')
            ->where('id', $topic_id)
            ->find();

        $result ['user'] = get_curl('user', '/user/get_user_information', ['id' => $result['user_id']])['data'];

        $result['comment'] = Db::name('comment')
            ->where('topic_id', $topic_id)
            ->paginate();

        $result['comment'] = $result['comment']->toArray()['data'];

        foreach ($result['comment'] as $k => $v) {
            if (isset($data['user_id'])) {
                $flag = Db::name('comment_like')
                    ->where('user_id', $data['user_id'])
                    ->where('comment_id', $v['id'])
                    ->where('ip', $_SERVER['REMOTE_ADDR'])
                    ->find();
                if ($flag) {
                    $result['comment'][$k]['flag'] = true;
                } else {
                    $result['comment'][$k]['flag'] = false;
                }
            } else {
                $result['comment'][$k]['flag'] = false;
            }
            $result['comment'][$k]['user'] = get_curl('user', '/user/get_user_information', ['id' => $v['user_id']])['data'];
            $result['comment'][$k]['reply'] = Db::name('reply')
                ->where('topic_id', $topic_id)
                ->where('comment_id', $v['id'])
                ->select();
            foreach ($result['comment'][$k]['reply'] as $key => $value) {
                $result['comment'][$k]['reply'][$key]['user'] = get_curl('user', '/user/get_user_information', ['id' => $value['user_id']])['data'];
            }
        }

        $msg['msg'] = '普通帖子详情获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台普通帖子评论
     * @description 前台普通帖子评论
     * @author 李锐志
     * @url /topic/topic/add_comment
     * @param name:token require:1 default: desc:token
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:content require:1 default: desc:内容
     * @param name:anchor require:1 default: desc:锚点
     */
    public function add_comment()
    {
        $data = input();
        $content_len = mb_strlen($data['content']);
        if ($content_len < 1 || $content_len > 50) {
            $msg['msg'] = '评论失败,内容长度:1~50';
            return $this->errorJson($msg);
        }
        $data['user_id'] = USERID;
        $data['create_time'] = time();
        $data['argument'] = $data['content'];

        $validate = new Validate([
            'token' => 'require',
            'topic_id' => 'require',
            'content' => 'require',
        ]);

        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        //更新帖子评论数量
        Db::name('topic')
            ->where('id', $data['topic_id'])
            ->setInc('discuss_num', 1);

        //用户增加积分
        get_curl('user', '/user/comment_add_integral', ['token' => $data['token']]);

        //添加积分记录
        Db::name('integral_record')
            ->insert([
                'integral_type' => 'comment',
                'integral' => $this->system_config('comment_integral'),
                'create_time' => time(),
                'user_id' => USERID
            ]);

        //给被评论用户发送信息
        $topic = Db::name('topic')
            ->where('id', $data['topic_id'])
            ->field('title,user_id')
            ->find();

        if ($topic['user_id'] !== USERID) {
            $arr = [
                'token' => $data['token'],
                'receive_user_id' => $topic['user_id'],
                'send_user_id' => USERID,
                'content' => $topic['title'] . '的帖子中评论了你：' . $data['content'],
                'create_time' => time(),
                'topic_id' => $data['topic_id'],
                'anchor' => $data['anchor']
            ];
            get_curl('message', '/message/add', $arr);
            $quantity = get_curl('message', '/message/id_get_message', ['user_id' => $topic['user_id']])['quantity'];
            get_curl('user', '/user/edit_message', ['id' => $topic['user_id'], 'quantity' => $quantity]);
        }

        //添加评论
        Db::name('comment')
            ->strict(false)
            ->insert($data);

        //更新帖子时间
        Db::name('topic')
            ->where('id', $data['topic_id'])
            ->setField('update_time', time());

        $msg['msg'] = '评论成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台普通帖子评论回复
     * @description 前台普通帖子评论回复
     * @author 李锐志
     * @url /topic/topic/add_reply
     * @param name:token require:1 default: desc:token
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:comment_id require:1 default: desc:评论id
     * @param name:content require:1 default: desc:内容
     * @param name:anchor require:1 default: desc:锚点
     */
    public function add_reply()
    {
        $data = input();
        $data['user_id'] = USERID;
        $data['create_time'] = time();
        $data['argument'] = $data['content'];

        $validate = new Validate([
            'token' => 'require',
            'topic_id' => 'require',
            'content' => 'require',
        ]);

        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        //对应评论的回复加一
        Db::name('comment')
            ->where('id', $data['comment_id'])
            ->setInc('reply_num', 1);

        //给被回复的用户发送信息
        $receive_user_id = Db::name('comment')
            ->where('id', $data['comment_id'])
            ->value('user_id');
        $topic = Db::name('topic')
            ->where('id', $data['topic_id'])
            ->value('title');

        if ($receive_user_id !== USERID) {
            $arr = [
                'token' => $data['token'],
                'receive_user_id' => $receive_user_id,
                'send_user_id' => USERID,
                'content' => $topic . '的帖子中回复了你：' . $data['content'],
                'create_time' => time(),
                'topic_id' => $data['topic_id'],
                'anchor' => $data['anchor']
            ];
            get_curl('message', '/message/add', $arr);
            $quantity = get_curl('message', '/message/id_get_message', ['user_id' => $receive_user_id])['quantity'];
            get_curl('user', '/user/edit_message', ['id' => $receive_user_id, 'quantity' => $quantity]);
        }

        $data['reply_user_id'] = $receive_user_id;
        //添加回复
        Db::name('reply')
            ->strict(false)
            ->insert($data);

        //更新帖子时间
        Db::name('topic')
            ->where('id', $data['topic_id'])
            ->setField('update_time', time());

        $msg['msg'] = '回复成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台评论点赞
     * @description 前台评论点赞
     * @author 李锐志
     * @url /topic/topic/comment_like
     * @param name:token require:1 default: desc:token
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:comment_id require:1 default: desc:评论id
     * @param name:anchor require:1 default: desc:锚点
     */
    public function comment_like()
    {
        $data = input();

        //获取用户id
        $data['user_id'] = USERID;

        //获取用户ip
        $data['ip'] = $_SERVER['REMOTE_ADDR'];

        //判断用户是否点过赞
        $result = Db::name('comment_like')
            ->where('user_id', $data['user_id'])
            ->where('ip', $data['ip'])
            ->where('comment_id', $data['comment_id'])
            ->find();

        if ($result) {
            $like_num = Db::name('comment')
                ->where('id', $data['comment_id'])
                ->value('like_num');
        } else {
            //插入点赞用户信息
            Db::name('comment_like')
                ->strict(false)
                ->insert($data);
            //更新该评论的点赞数据
            Db::name('comment')
                ->where('id', $data['comment_id'])
                ->setInc('like_num', 1);
            //获取最新点赞数
            $like_num = Db::name('comment')
                ->where('id', $data['comment_id'])
                ->value('like_num');

            //获取该评论的用户id
            $receive_user_id = Db::name('comment')
                ->where('id', $data['comment_id'])
                ->value('user_id');

            //自己给自己点赞不用发信息
            if (!($receive_user_id === USERID)) {
                $arr = [
                    'token' => $data['token'],
                    'receive_user_id' => $receive_user_id,
                    'send_user_id' => USERID,
                    'content' => '赞了你的评论',
                    'create_time' => time(),
                    'topic_id' => $data['topic_id'],
                    'anchor' => $data['anchor']
                ];
                get_curl('message', '/message/add', $arr);
                $quantity = get_curl('message', '/message/id_get_message', ['user_id' => $receive_user_id])['quantity'];
                get_curl('user', '/user/edit_message', ['id' => $receive_user_id, 'quantity' => $quantity]);
            }
        }

        $msg['msg'] = '点赞成功';
        $msg['like_view'] = $like_num;
        return $this->successJson($msg);
    }

    /**
     * @title 前台获取特殊帖子分类
     * @description 前台获取特殊帖子分类
     * @author 李锐志
     * @url /topic/topic/special_topic_category
     */
    public function special_topic_category()
    {
        $result = Db::name('topic_category')
            ->where('parent_id', 0)
            ->where('status', 1)
            ->where('name', '设计博弈')
            ->find();

        $result['son_category'] = Db::name('topic_category')
            ->where('parent_id', $result['id'])
            ->where('status', 1)
            ->select();

        $msg['msg'] = '特殊帖子分类获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子图片注释
     * @description 前台特殊帖子图片注释
     * @author 李锐志
     * @url /topic/topic/special_topic_img
     * @param name:img_url require:1 default: desc:图片路径
     * @param name:img_notation require:1 default: desc:注释
     */
    public function special_topic_img()
    {
        $data = input();

        $validate = new Validate([
            'img_url' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        Db::name('comment_img')
            ->strict(false)
            ->insert($data);

        $msg['msg'] = '上传成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子发帖
     * @description 前台特殊帖子发帖
     * @author 李锐志
     * @url /topic/topic/special_topic_publish
     * @param name:token require:1 default: desc:token
     * @param name:title require:1 default: desc:标题
     * @param name:category_id require:1 default: desc:分类id
     * @param name:content require:1 default: desc:内容
     * @param name:img_annotation require:1 default: desc:图片和注释(传json数据)
     * @param name:main_img_url require:1 default: desc:封面
     */
    public function special_topic_publish()
    {
        $data = input();
        $title_len = mb_strlen($data['title']);
        if ($title_len < 1 || $title_len > 60) {
            $msg['msg'] = '特殊帖子发帖失败,标题长度:1~60';
            return $this->errorJson($msg);
        }
        $validate = new Validate([
            'token' => 'require',
            'title' => 'require',
            'category_id' => 'require',
            'content' => 'require',
            'img_annotation' => 'require',
            'main_img_url' => 'require'
        ]);

        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->successJson($msg);
        }

        $insert = [
            'title' => $data['title'],
            'user_id' => USERID,
            'content' => $data['content'],
            'category_id' => $data['category_id'],
            'topic_type' => 'special_topic',
            'main_img_url' => $data['main_img_url'],
            'is_mentality' => 0,
            'create_time' => time(),
            'update_time' => time()
        ];

        $topic_id = Db::name('topic')
            ->insertGetId($insert);

        $img_annotation = json_decode($data['img_annotation'], true)['img_annotation'];

        foreach ($img_annotation as $k => $v) {
            Db::name('comment_img')
                ->insert([
                   'img_url' => $v['img_url'],
                   'img_notation' => $v['img_notation'],
                   'topic_id' => $topic_id,
                ]);
        }

        //发帖添加积分
        get_curl('user', '/user/topic_add_integral', ['token' => $data['token']]);

        $msg['msg'] = '特殊帖子发帖成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子评论
     * @description 前台特殊帖子评论
     * @author 李锐志
     * @url /topic/topic/add_special_comment
     * @param name:token require:1 default: desc:token
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:comment_img_id require:1 default: desc:评论图片id
     * @param name:argument require:1 default: desc:论点
     * @param name:saytext require:1 default: desc:依据
     * @param name:img_arr require:1 default: desc:图片数组
     * @param name:anchor require:1 default: desc:锚点
     */
    public function add_special_comment()
    {
        $data = input();
        $argument_len = mb_strlen($data['argument']);
        if ($argument_len < 1 || $argument_len > 50) {
            $msg['msg'] = '评论失败,论点长度:1~50';
            return $this->errorJson($msg);
        }
        $validate = new Validate([
            'token' => 'require',
            'topic_id' => 'require',
            'comment_img_id' => 'require',
            'argument' => 'require',
            'saytext' => 'require',
            'anchor' => 'require',
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        if (isset($data['img_arr'])) {
            $img_arr = json_decode($data['img_arr']);
            $img_url = implode(',', $img_arr);
        } else {
            $img_url = null;
        }

        $insert = [
            'topic_id' => $data['topic_id'],
            'comment_img_id' => $data['comment_img_id'],
            'user_id' => USERID,
            'argument' => $data['argument'],
            'saytext' => $data['saytext'],
            'img_url' => $img_url,
            'create_time' => time()
        ];

        Db::name('comment')
            ->insert($insert);

        //更新帖子评论数量
        Db::name('topic')
            ->where('id', $data['topic_id'])
            ->setInc('discuss_num', 1);

        //用户增加积分
        get_curl('user', '/user/comment_add_integral', ['token' => $data['token']]);

        //添加积分记录
        Db::name('integral_record')
            ->insert([
                'integral_type' => 'comment',
                'integral' => $this->system_config('comment_integral'),
                'create_time' => time(),
                'user_id' => USERID
            ]);

        //给被评论用户发送信息
        $topic = Db::name('topic')
            ->where('id', $data['topic_id'])
            ->field('title,user_id')
            ->find();

        if ($topic['user_id'] !== USERID) {
            $arr = [
                'token' => $data['token'],
                'receive_user_id' => $topic['user_id'],
                'send_user_id' => USERID,
                'content' => $topic['title'] . '的帖子中评论了你：' . $data['argument'],
                'create_time' => time(),
                'topic_id' => $data['topic_id'],
                'anchor' => $data['anchor']
            ];
            get_curl('message', '/message/add', $arr);
            $quantity = get_curl('message', '/message/id_get_message', ['user_id' => $topic['user_id']])['quantity'];
            get_curl('user', '/user/edit_message', ['id' => $topic['user_id'], 'quantity' => $quantity]);
        }

        //更新帖子时间
        Db::name('topic')
            ->where('id', $data['topic_id'])
            ->setField('update_time', time());

        $msg['msg'] = '评论成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子回复
     * @description 前台特殊帖子回复
     * @author 李锐志
     * @url /topic/topic/add_special_reply
     * @param name:token require:1 default: desc:token
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:comment_id require:1 default: desc:评论id
     * @param name:comment_img_id require:1 default: desc:评论图片id
     * @param name:argument require:1 default: desc:论点
     * @param name:saytext require:1 default: desc:依据
     * @param name:img_arr require:1 default: desc:图片数组
     * @param name:anchor require:1 default: desc:锚点
     */
    public function add_special_reply()
    {
        $data = input();

        $validate = new Validate([
            'token' => 'require',
            'topic_id' => 'require',
            'comment_id' => 'require',
            'comment_img_id' => 'require',
            'argument' => 'require',
            'saytext' => 'require',
            'anchor' => 'require',
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        if (isset($data['img_arr'])) {
            $img_arr = json_decode($data['img_arr']);
            $img_url = implode(',', $img_arr);
        } else {
            $img_url = null;
        }

        $reply_user_id = Db::name('topic')
            ->where('id', $data['topic_id'])
            ->value('user_id');

        $insert = [
            'topic_id' => $data['topic_id'],
            'comment_img_id' => $data['comment_img_id'],
            'comment_id' => $data['comment_id'],
            'user_id' => USERID,
            'reply_user_id' => $reply_user_id,
            'argument' => $data['argument'],
            'saytext' => $data['saytext'],
            'img_url' => $img_url,
            'create_time' => time()
        ];

        Db::name('reply')
            ->insert($insert);

        //对应评论的回复加一
        Db::name('comment')
            ->where('id', $data['comment_id'])
            ->setInc('reply_num', 1);

        //给被回复的用户发送信息
        $receive_user_id = Db::name('comment')
            ->where('id', $data['comment_id'])
            ->value('user_id');
        $topic = Db::name('topic')
            ->where('id', $data['topic_id'])
            ->value('title');

        if ($receive_user_id !== USERID) {
            $arr = [
                'token' => $data['token'],
                'receive_user_id' => $receive_user_id,
                'send_user_id' => USERID,
                'content' => $topic . '的帖子中回复了你：' . $data['argument'],
                'create_time' => time(),
                'topic_id' => $data['topic_id'],
                'anchor' => $data['anchor']
            ];
            get_curl('message', '/message/add', $arr);
            $quantity = get_curl('message', '/message/id_get_message', ['user_id' => $receive_user_id])['quantity'];
            get_curl('user', '/user/edit_message', ['id' => $receive_user_id, 'quantity' => $quantity]);
        }

        //更新帖子时间
        Db::name('topic')
            ->where('id', $data['topic_id'])
            ->setField('update_time', time());

        $msg['msg'] = '回复成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子详情
     * @description 前台特殊帖子详情
     * @author 李锐志
     * @url /topic/topic/special_topic_details
     * @param name:user_id require:0 default: desc:用户id
     * @param name:topic_id require:1 default: desc:帖子id
     */
    public function special_topic_details()
    {
        $data = input();

        //防止恶意刷浏览量，使用session来判断当前用户是否第一次浏览
        if(!is_dir('./tmp/'))mkdir ('./tmp/', 0700);
        session_save_path('./tmp/');
        session_start();
        if (empty($_SESSION['noRepeatId']) || $_SESSION['noRepeatId'] !== $_SERVER['REMOTE_ADDR'] . $data['topic_id']) {
            Db::name('topic')
                ->where('id', $data['topic_id'])
                ->setInc('view_num', 1);
            $_SESSION['noRepeatId'] = $_SERVER['REMOTE_ADDR'] . $data['topic_id'];
        }

        $topic = Db::name('topic')
            ->where('id', input('topic_id'))
            ->field('img_url,annotation', true)
            ->withAttr('user', function ($value, $data) {
                return get_curl('user', '/user/get_user_information', ['id' => $data['user_id']])['data'];
            })
            ->find();

        $topic['img'] = Db::name('comment_img')
            ->where('topic_id', $topic['id'])
            ->select();

        //第一层:帖子图片
        foreach ($topic['img'] as $k => $v) {
            $topic['img'][$k]['comment'] = Db::name('comment')
                ->where('topic_id', $topic['id'])
                ->where('comment_img_id', $v['id'])
                ->withAttr('user', function ($value, $data) {
                    return get_curl('user', '/user/get_user_information', ['id' => $data['user_id']])['data'];
                })
                ->field('id,user_id,argument,saytext,like_num,create_time,reply_num')
                ->select();
            //第二层:帖子评论
            foreach ($topic['img'][$k]['comment'] as $k2 => $v2) {
                if (isset($data['user_id'])) {
                    $flag = Db::name('comment_like')
                        ->where('user_id', $data['user_id'])
                        ->where('comment_id', $v2['id'])
                        ->where('ip', $_SERVER['REMOTE_ADDR'])
                        ->find();
                    if ($flag) {
                        $topic['img'][$k]['comment'][$k2]['flag'] = true;
                    } else {
                        $topic['img'][$k]['comment'][$k2]['flag'] = false;
                    }
                } else {
                    $topic['img'][$k]['comment'][$k2]['flag'] = false;
                }
                $topic['img'][$k]['comment'][$k2]['reply'] = Db::name('reply')
                    ->where('topic_id', $topic['id'])
                    ->where('comment_id', $v2['id'])
                    ->withAttr('user', function ($value, $data) {
                        return get_curl('user', '/user/get_user_information', ['id' => $data['user_id']])['data'];
                    })
                    ->field('id,user_id,argument,saytext,like_num,create_time')
                    ->select();
                //第三层:帖子评论的回复
                foreach ($topic['img'][$k]['comment'][$k2]['reply'] as $k3 => $v3) {
                    if (isset($data['user_id'])) {
                        $flag = Db::name('reply_like')
                            ->where('user_id', $data['user_id'])
                            ->where('reply_id', $v3['id'])
                            ->where('ip', $_SERVER['REMOTE_ADDR'])
                            ->find();
                        if ($flag) {
                            $topic['img'][$k]['comment'][$k2]['reply'][$k3]['flag'] = true;
                        } else {
                            $topic['img'][$k]['comment'][$k2]['reply'][$k3]['flag'] = false;
                        }
                    } else {
                        $topic['img'][$k]['comment'][$k2]['reply'][$k3]['flag'] = false;
                    }
                }
            }
        }

        $msg['msg'] = '特殊帖子详情返回成功';
        $msg['data'] = $topic;
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子评论详情
     * @description 前台特殊帖子评论详情
     * @author 李锐志
     * @url /topic/topic/special_topic_comment_details
     * @param name:user_id require:1 default: desc:用户id
     * @param name:comment_id require:1 default: desc:评论id
     */
    public function special_topic_comment_details()
    {
        $data = input();
        $result = Db::name('comment')
            ->where('id', input('comment_id'))
            ->withAttr('comment_img_url', function ($value, $data) {
                return Db::name('comment_img')
                    ->where('id', $data['comment_img_id'])
                    ->value('img_url');
            })
            ->withAttr('img', function ($value, $data) {
                return explode(',', $data['img_url']);
            })
            ->withAttr('user', function ($value, $data) {
                return get_curl('user', '/user/get_user_information', ['id' => $data['user_id']])['data'];
            })
            ->find();
        if (isset($data['user_id'])) {
            $flag = Db::name('comment_like')
                ->where('user_id', $data['user_id'])
                ->where('comment_id', $result['id'])
                ->where('ip', $_SERVER['REMOTE_ADDR'])
                ->find();
            if ($flag) {
                $result['flag'] = true;
            } else {
                $result['flag'] = false;
            }
        } else {
            $result['flag'] = false;
        }

        $msg['msg'] = '评论详情获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台特殊帖子回复详情
     * @description 前台特殊帖子回复详情
     * @author 李锐志
     * @url /topic/topic/special_topic_reply_details
     * @param name:user_id require:1 default: desc:用户id
     * @param name:reply_id require:1 default: desc:回复id
     */
    public function special_topic_reply_details()
    {
        $data = input();
        $result = Db::name('reply')
            ->where('id', input('reply_id'))
            ->withAttr('comment_img_url', function ($value, $data) {
                return Db::name('comment_img')
                    ->where('id', $data['comment_img_id'])
                    ->value('img_url');
            })
            ->withAttr('img', function ($value, $data) {
                return explode(',', $data['img_url']);
            })
            ->withAttr('user', function ($value, $data) {
                return get_curl('user', '/user/get_user_information', ['id' => $data['user_id']])['data'];
            })
            ->find();

        if (isset($data['user_id'])) {
            $flag = Db::name('reply_like')
                ->where('user_id', $data['user_id'])
                ->where('reply_id', $result['id'])
                ->where('ip', $_SERVER['REMOTE_ADDR'])
                ->find();
            if ($flag) {
                $result['flag'] = true;
            } else {
                $result['flag'] = false;
            }
        } else {
            $result['flag'] = false;
        }
        $msg['msg'] = '回复详情获取成功';
        $msg['data'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台回复点赞
     * @description 前台回复点赞
     * @author 李锐志
     * @url /topic/topic/reply_like
     * @param name:token require:1 default: desc:token
     * @param name:topic_id require:1 default: desc:帖子id
     * @param name:reply_id require:1 default: desc:回复id
     * @param name:anchor require:1 default: desc:锚点
     */
    public function reply_like()
    {
        $data = input();

        //获取用户id
        $data['user_id'] = USERID;

        //获取用户ip
        $data['ip'] = $_SERVER['REMOTE_ADDR'];

        //判断用户是否点过赞
        $result = Db::name('reply_like')
            ->where('user_id', $data['user_id'])
            ->where('ip', $data['ip'])
            ->where('reply_id', $data['reply_id'])
            ->find();

        if ($result) {
            $like_num = Db::name('reply')
                ->where('id', $data['reply_id'])
                ->value('like_num');
        } else {
            //插入点赞用户信息
            Db::name('reply_like')
                ->strict(false)
                ->insert($data);
            //更新该评论的点赞数据
            Db::name('reply')
                ->where('id', $data['reply_id'])
                ->setInc('like_num', 1);
            //获取最新点赞数
            $like_num = Db::name('reply')
                ->where('id', $data['reply_id'])
                ->value('like_num');

            //获取该评论的用户id
            $receive_user_id = Db::name('reply')
                ->where('id', $data['reply_id'])
                ->value('user_id');

            //自己给自己点赞不用发信息
            if (!($receive_user_id === USERID)) {
                $arr = [
                    'token' => $data['token'],
                    'receive_user_id' => $receive_user_id,
                    'send_user_id' => USERID,
                    'content' => '赞了你的回复',
                    'create_time' => time(),
                    'topic_id' => $data['topic_id'],
                    'anchor' => $data['anchor']
                ];
                get_curl('message', '/message/add', $arr);
                $quantity = get_curl('message', '/message/id_get_message', ['user_id' => $receive_user_id])['quantity'];
                get_curl('user', '/user/edit_message', ['id' => $receive_user_id, 'quantity' => $quantity]);
            }
        }

        $msg['msg'] = '点赞成功';
        $msg['like_view'] = $like_num;
        return $this->successJson($msg);
    }
    /**
     * @title 用户信息列表
     * @description 选择器用户列表
     * @author 宋晓文
     * @url /topic/topic/get_user_msg
     * @method *
     */
    function get_user_msg(){
        $user = Db::table('zb_user.tp_user')->field(['id','nickname','phone'])->select();
        $data = array();
        foreach ($user as $k=>$v){
            $temp['id'] = $v['id'];
            $temp['value'] = $v['nickname']." - ".$v['phone'];
            array_push($data,$temp);
        }

        $msg['list'] = $data;
        return $this->successJson($msg);
    }


}
